/**
 * 右键菜单管理器
 * addItem():添加自定义菜单项
 * removeItem():移除某自定义菜单项
 * removeAllItem():移除所有自定义菜单项
 * */
package utils.core
{
    import flash.display.InteractiveObject;
    import flash.display.Sprite;
    import flash.events.ContextMenuEvent;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;

    public class ContexMenuManager extends Sprite {
        

        public function ContexMenuManager() 
        {
	         
        }
        
        /**为交互对象添加右键菜单项
        * cb回调目前设计为只能传一个参数
        * */
        public static function addItem(menuOwner:InteractiveObject,itemLabel:String,cb:Function=null,param:*=null):void
        {
        	if(menuOwner.contextMenu == null)
        	{
        		var myContextMenu:ContextMenu
	            myContextMenu = new ContextMenu();
	            myContextMenu.hideBuiltInItems();
	            
	            myContextMenu.builtInItems.print = false;
	            myContextMenu.builtInItems.save = false;
	            myContextMenu.builtInItems.rewind = false;
	            myContextMenu.builtInItems.zoom = false;
	            myContextMenu.builtInItems.play = false;
	            myContextMenu.builtInItems.loop = false;
	            myContextMenu.builtInItems.forwardAndBack = false;
	            myContextMenu.builtInItems.quality = false;
	            
	            menuOwner.contextMenu = myContextMenu;
        	}
        	var item:ContextMenuItem = new ContextMenuItem(itemLabel);
            menuOwner.contextMenu.customItems.push(item);
            if(cb == null)
            	item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onItemSelect);
            else
            {
            	item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, Params.add(onItemSelect,cb,param));
            }
            	
        }
        
        //删除某个自定义菜单项
        public static function removeItem(menuOwner:InteractiveObject,itemLabel:String):void
        {
        	if(menuOwner.contextMenu == null)
        		return;
        	var item:ContextMenuItem;
        	var i:uint = 0;
        	for each(item in menuOwner.contextMenu.customItems)
        	{
        		if(item.caption == itemLabel)
        		{
        			//menuOwner.contextMenu.removeItemAt(i);
        			menuOwner.contextMenu.customItems.splice(i,1);
        			break;
        		}
        		i++;
        	}
        	
        }
        
        //删除所有自定义菜单项
        public static function removeAllItem(menuOwner:InteractiveObject):void
        {
        	if(menuOwner.contextMenu == null)
        		return;
        	menuOwner.contextMenu.customItems.length = 0;
        }

		//菜单项被点击
        private static function onItemSelect(event:ContextMenuEvent,cb:Function=null,param:*=null):void 
        {
            trace("menuItemSelect" + event);
            if(cb != null)
            {
            	cb.call(param);
            }
        }

    }
}